<?php

namespace app\admin\controller\wechat;

use app\admin\controller\Base;
use think\Db;

/**
 * 用户组管理
 * Class GroupController
 * @package app\admin\controller\auth
 */
class Keyword extends Base
{

    /**
     * 主页面
     * @return mixed
     */
    public function index()
    {
        return view();
    }


    public function list_msg()
    {
        //dump($map);
        $data['count'] = Db::name('msg_wechat')
            ->where('id', '>', 0)
            ->count();
        // echo Db::name('supplier')->where($map)->fetchSql(true)->select();
        $data['data'] = Db::name('msg_wechat')
            ->order('id')
            //->field(['id'=>'value','area'=>'name','pid'])
            ->where('id', '>', 0)
            ->select();
        //  dump($data['data']);
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    public function lists()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        unset($post['page']);
        unset($post['limit']);
        $data['count'] = Db::view('keyword','keyword,id')
            ->view('msg_wechat','title','keyword.msg_id=msg_wechat.id')
            ->where($post)
            ->count();

        $data['data'] = Db::view('keyword','keyword,id')
            ->view('msg_wechat','title','keyword.msg_id=msg_wechat.id')
            ->where($post)
            // ->fetchSql(true)
            ->order('id desc')->page(input("page"), input('limit'))->select();

        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }


    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            if (!isset($post['keyword']) || empty($post['keyword'])) {
                $this->error(' 关键字不能为空');
            }
            if (!isset($post['msg_id']) || empty($post['msg_id'])) {
                $this->error('图文消息必须选择');
            }

            if (Db::name('keyword')->insert($post) !== false) {
                $this->success('操作成功');
            }
            $this->error('添加失败');

        } else {

            return view();
        }
    }


    public function delete()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();

            if (!isset($post['id'])) {
                $this->error('请求参数错误');
            }

            if (Db::name('keyword')->delete($post) !== false) {
                $this->success('操作成功!');
            }
            $this->error('更新失败');
        }
    }


    public function edit()
    {
        if ($this->request->isPost()) {

            $post = $this->request->param();
            if (!isset($post['title']) || empty($post['title'])) {
                $this->error(' 标题不能为空');
            }
            if (!isset($post['url']) || empty($post['url'])) {
                $this->error('链接不能为空');
            }
            if (!isset($post['img'])) {
                $this->error('图片不能为空');
            }
            if (Db::name('keyword')->update($post) !== false) {
                $this->success('操作成功');
            }
            $this->error('编辑失败');

        } else {
            $info = Db::name('keyword')
                ->where('id', input('id'))
                ->find();
            $this->assign('info', $info);
            return view('add');
        }

    }


    public function editcategory()
    {
        if ($this->request->isPost()) {

            $post = $this->request->param();
            if (!isset($post['title']) || empty($post['title'])) {
                $this->error(' 名称不能为空');
            }


            if (!isset($post['content']) || empty($post['content'])) {
                $this->error('内容不能为空');
            }

            if (Db::name('story')->update($post) !== false) {
                $this->success('操作成功');
            }
            $this->error('编辑失败');

        } else {
            $info = Db::name('story')
                ->where('id', input('id'))
                ->find();
            $this->assign('info', $info);
            $category = Db::name('category')
                ->where('model_id', 2)
                ->select();
            $this->assign('category',$category);
            return view('add');
        }

    }
}